/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.Comments;
import edu.cmu.fithub.db.bean.DisplayComments;
import edu.cmu.fithub.db.bean.Friend;
import edu.cmu.fithub.db.bean.GroupMembers;
import edu.cmu.fithub.db.bean.DisplayPosts;
import edu.cmu.fithub.db.bean.Posts;
import edu.cmu.fithub.db.bean.User;

public class DisplayHomeAction extends Action {

	public DisplayHomeAction() {
	}

	public String getName() {
		return "showHome.do";
	}

	public String perform(HttpServletRequest request) {

		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		User user = (User) request.getSession().getAttribute("user");

		// retrieve friends

		/**
		 * retrieve posts 1. first retrieve all posts made by user 2. retrieve
		 * all posts made by users he is following 3. retrieve all posts made in
		 * groups he is following
		 */
		Session dbSession = DBHelper.getSessionFactory().openSession();
		List list = dbSession.createCriteria(Friend.class)
				.add(Restrictions.eq("userId", user.getUserId())).list();
		Friend[] friendList = new Friend[list.size()];
		friendList = (Friend[]) list.toArray(friendList);
		request.setAttribute("friendList", friendList);

		List<Posts> postList = dbSession.createCriteria(Posts.class)
				.add(Restrictions.eq("user.userId", user.getUserId()))
				.add(Restrictions.eq("groupId", -1)).list();
		List groupList = dbSession.createCriteria(GroupMembers.class)
				.add(Restrictions.eq("userId", user.getUserId())).list();
		for (int i = 0; i < groupList.size(); i++) {
			List<Posts> groupPostList = dbSession
					.createCriteria(Posts.class)
					.add(Restrictions.eq("groupId", ((GroupMembers) groupList
							.get(i)).getGroup().getGroupId())).list();
			postList.addAll(groupPostList);
		}

		// find posts for all friends
		for (int i = 0; i < friendList.length; i++) {
			List<Posts> friendPostList = dbSession
					.createCriteria(Posts.class)
					.add(Restrictions.eq("user.userId",
							friendList[i].getFriendId()))
					.add(Restrictions.eq("groupId", -1)).list();
			postList.addAll(friendPostList);
		}
		
		Collections.sort(postList);
		
		List<DisplayPosts> orderedPostsList = new ArrayList<DisplayPosts>();
		
		for(int i=0; i<postList.size(); i++) {
			Posts posts = postList.get(i);
	        DisplayPosts orderedPosts = new DisplayPosts();
	        orderedPosts.setGroupId(posts.getGroupId());
	        orderedPosts.setPost(posts.getPost());
	        orderedPosts.setPostId(posts.getPostId());
	        orderedPosts.setPostType(posts.getPostType());
	        orderedPosts.setUser(posts.getUser());
	        
	        SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss aaa");
	        df.setTimeZone(TimeZone.getTimeZone(user.getTimeZone()));
	        String date = df.format(posts.getDate());
	        orderedPosts.setDate(date);
	        
			List<Comments> comments = posts.getComments();
			List<DisplayComments> displayComments = new ArrayList<DisplayComments>();
			for(int j=0; j<comments.size(); j++) {
				DisplayComments comments2 = new DisplayComments();
				comments2.setComment(comments.get(j).getComment());
				comments2.setCommentId(comments.get(j).getCommentId());
				comments2.setPost(posts);
				comments2.setUser(comments.get(j).getUser());
				date = df.format(comments.get(j).getDate());
				comments2.setDate(date);
				displayComments.add(comments2);
			}
			orderedPosts.setComments(displayComments);
			orderedPostsList.add(orderedPosts);
		}

		DisplayPosts[] stories = new DisplayPosts[orderedPostsList.size()];
		stories = (DisplayPosts[]) orderedPostsList.toArray(stories);
		request.setAttribute("stories", stories);
		return "home.jsp";
	}
}
